package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.dto.FieldInfo;
import com.sg.common.exception.BizException;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.*;
import com.sg.entity.OmsConfItemOptionRelationship;
import com.sg.mapper.OmsConfItemOptionRelationshipMapper;
import com.sg.service.base.component.MOmsConfItemOptionRelationshipService;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsConfItemOptionRelationshipServiceImpl
        extends ServiceImpl<OmsConfItemOptionRelationshipMapper, OmsConfItemOptionRelationship>
        implements MOmsConfItemOptionRelationshipService {

    @Autowired
    OmsConfItemOptionRelationshipMapper omsConfItemOptionRelationshipMapper;

    /**
     * 2-3-03批量查询配置项下选项关系列表[1996]
     * gen by moon at 9/9/2022, 4:22:49 AM
     */
    @Trace(operationName = "2-3-03批量查询配置项下选项关系列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfItemOptionRelationship> batchQueryConfItemOptionList(BatchQueryConfItemOptionListReq reqDto) {

        try {
            QueryWrapper<OmsConfItemOptionRelationship> queryWrapper = new QueryWrapper<>();

            if (reqDto.getConfItemOptionList() == null || reqDto.getConfItemOptionList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("conf_item_id", reqDto.getConfItemOptionList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfItemOptionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfItemOptionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfItemOptionRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsConfItemOptionRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-03批量新增配置项下选项关系[2154]
     * gen by moon at 9/9/2022, 4:22:51 AM
     */
    @Trace(operationName = "2-3-03批量新增配置项下选项关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddConfItemOption(List<OmsConfItemOptionRelationship> omsConfItemOptionRelationship) {

        if (omsConfItemOptionRelationship == null || omsConfItemOptionRelationship.size() == 0) {
            return false;
        }

        return super.saveBatch(omsConfItemOptionRelationship.stream().map(item -> {
            item.setConfOptionRshipId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setSubjectLifeCycle("EDITING");
            item.setIsArchive("FALSE");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 2-3-03批量删配置项下选项关系[2062]
     * gen by moon at 9/9/2022, 4:22:51 AM
     */
    @Trace(operationName = "2-3-03批量删配置项下选项关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteConfItemOption(List<String> reqDto) {

        if (reqDto == null || reqDto.size() == 0) {
            return false;
        }

        return super.removeByIds(reqDto);
    }

    /**
     * 2-3-03批量修改配置项下选项关系[2044]
     * gen by moon at 9/9/2022, 4:22:50 AM
     */
    @Trace(operationName = "2-3-03批量修改配置项下选项关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchUpdateConfItemOption(List<OmsConfItemOptionRelationship> omsConfItemOptionRelationship) {

        if (omsConfItemOptionRelationship == null || omsConfItemOptionRelationship.size() == 0) {
            return false;
        }
        return super.saveOrUpdateBatch(omsConfItemOptionRelationship);

    }

    /**
     * 2-3-03查询配置项下选项关系列表[1918]
     * gen by moon at 9/9/2022, 4:22:49 AM
     */
    @Trace(operationName = "2-3-03查询配置项下选项关系列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfItemOptionRelationship> queryConfItemOptionList(QueryConfItemOptionListReq reqDto) {

        try {
            QueryWrapper<OmsConfItemOptionRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfItemOptionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfItemOptionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfItemOptionRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsConfItemOptionRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-03删除配置项下选项关系[1917]
     * gen by moon at 9/9/2022, 4:22:48 AM
     */
    @Trace(operationName = "2-3-03删除配置项下选项关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteConfItemOption(DeleteConfItemOptionReq reqDto) {
        reqDto.setSubjectLifeCycle("EDITING");
        try {
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            return super.removeByMap(columnMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-03修改配置项下选项关系[1916]
     * gen by moon at 9/9/2022, 4:22:47 AM
     */
    @Trace(operationName = "2-3-03修改配置项下选项关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateConfItemOption(OmsConfItemOptionRelationship omsConfItemOptionRelationship) {
        omsConfItemOptionRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfItemOptionRelationship != null && omsConfItemOptionRelationship.getSubjectLifeCycle() != null && omsConfItemOptionRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfItemOptionRelationship.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsConfItemOptionRelationship != null && omsConfItemOptionRelationship.getSubjectLifeCycle() != null && omsConfItemOptionRelationship.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsConfItemOptionRelationship.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsConfItemOptionRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfItemOptionRelationship != null && omsConfItemOptionRelationship.getSubjectLifeCycle() != null && omsConfItemOptionRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfItemOptionRelationship.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsConfItemOptionRelationship != null && omsConfItemOptionRelationship.getSubjectLifeCycle() != null && omsConfItemOptionRelationship.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsConfItemOptionRelationship.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsConfItemOptionRelationship);
    }

    /**
     * 2-3-03查询配置项下选项关系[1915]
     * gen by moon at 9/9/2022, 4:22:47 AM
     */
    @Trace(operationName = "2-3-03查询配置项下选项关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsConfItemOptionRelationship queryConfItemOptionDetail(QueryConfItemOptionDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsConfItemOptionRelationship> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsConfItemOptionRelationship> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsConfItemOptionRelationship retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-03新增配置项下选项关系[1914]
     * gen by moon at 9/9/2022, 4:22:46 AM
     */
    @Trace(operationName = "2-3-03新增配置项下选项关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addConfItemOption(OmsConfItemOptionRelationship omsConfItemOptionRelationship) {
        omsConfItemOptionRelationship.setConfOptionRshipId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsConfItemOptionRelationship.setSubjectLifeCycle("EDITING");
        omsConfItemOptionRelationship.setIsArchive("FALSE");
        omsConfItemOptionRelationship.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsConfItemOptionRelationship.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfItemOptionRelationship != null && omsConfItemOptionRelationship.getSubjectLifeCycle() != null && omsConfItemOptionRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfItemOptionRelationship.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsConfItemOptionRelationship.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsConfItemOptionRelationship.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfItemOptionRelationship != null && omsConfItemOptionRelationship.getSubjectLifeCycle() != null && omsConfItemOptionRelationship.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfItemOptionRelationship.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsConfItemOptionRelationship.setIsValid("FALSE");
        omsConfItemOptionRelationship.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsConfItemOptionRelationship.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsConfItemOptionRelationship)) {
            return omsConfItemOptionRelationship.getConfOptionRshipId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-3-03批量查询配置项下选项关系[4162]
     * gen by moon at 9/9/2022, 4:22:52 AM
     */
    @Trace(operationName = "2-3-03批量查询配置项下选项关系")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfItemOptionRelationship> batchQueryConfItemOption(BatchQueryConfItemOptionReq reqDto) {

        try {
            QueryWrapper<OmsConfItemOptionRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getConfItemOptionList() == null || reqDto.getConfItemOptionList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("conf_option_code", reqDto.getConfItemOptionList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfItemOptionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfItemOptionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfItemOptionRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsConfItemOptionRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-03批量查询配置项下选项关系列表ByIds[4412]
     * gen by moon at 9/9/2022, 4:22:53 AM
     */
    @Trace(operationName = "2-3-03批量查询配置项下选项关系列表ByIds")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfItemOptionRelationship> batchQueryConfItemOptionListByIds(BatchQueryConfItemOptionListByIdsReq reqDto) {

        try {
            QueryWrapper<OmsConfItemOptionRelationship> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getConfItemOptionList() == null || reqDto.getConfItemOptionList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("conf_option_rship_id", reqDto.getConfItemOptionList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfItemOptionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfItemOptionRelationship.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfItemOptionRelationship.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsConfItemOptionRelationship>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
